package com.noahplat.person.service.impl;

import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.person.mapper.PersonBaseMapper;
import com.noahplat.person.pojo.PersonBase;
import com.noahplat.person.service.PersonBaseService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

/**
 * @ClassName: PersonBaseServiceImpl
 * @Description: 人员基本信息实现类
 * @Author 李景星
 * @Date 2021/3/6
 * @Version 1.0
 */
@Service
public class PersonBaseServiceImpl implements PersonBaseService {

    @Autowired
    private PersonBaseMapper personBaseMapper;

    /**
     * 查询人员基本信息
     * @param pid
     * @return
     */
    @Override
    public Result getPersonBase(Long pid) {
        Example example = new Example(PersonBase.class);
        example.createCriteria().andEqualTo("pid", pid);
        PersonBase personBase = personBaseMapper.selectOneByExample(example);
        //判断库表中是否有数据，若无数据，说明没有初始化人员基本信息，这时也需要正常给前端返回数据,同时初始化数据库
        if(ObjectUtils.isEmpty(personBase)){
            PersonBase newBase = new PersonBase(pid, "", null, "", "", "", "");
            personBaseMapper.insert(newBase);
            return new Result(true, StatusCode.OK, "查询数据成功", newBase);
        }
        return new Result(true, StatusCode.OK, "查询数据成功", personBase);
    }

    /**
     * 更新人员基本信息
     * @param personBase
     * @return
     */
    @Override
    public Result updatePersonBase(PersonBase personBase) {
        int res = personBaseMapper.updateByPrimaryKeySelective(personBase);
        if(res > 0){
            return new Result(true, StatusCode.OK, "人员基本信息更新成功");
        }
        return new Result(false, StatusCode.ERROR, "人员基本信息更新失败");
    }
}
